package day02;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

// @WebServlet("/testRequest")
// @WebServlet("/test/request")
// @WebServlet("/test/*")
@WebServlet("*.test")
public class TestRequestServlet extends HttpServlet {


    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("TestRequestServlet.service()");

        //应用名
        System.out.println(request.getContextPath());

        //url-pattern
        System.out.println(request.getServletPath());

        //地址栏
        System.out.println(request.getRequestURL());

        //contextPath+servletPath
        System.out.println(request.getRequestURI());

        //告诉容器，解析请求体中的数据使用utf-8编码
        request.setCharacterEncoding("utf-8");

        //告诉容器，响应体中的数据是utf-8编码
        response.setCharacterEncoding("utf-8");

        /*
            告诉容器，响应体中的数据格式是text/html（文本/html格式）,字符编码时utf-8
            以utf-8编码传输给客户端
            告诉浏览器使用utf-8编码解析响应体并打开
         */
        response.setContentType("text/html;charset=utf-8");

        //获得请求方式
        System.out.println(request.getMethod());

        //获得客户端ip地址
        System.out.println(request.getRemoteAddr());

        //获得请求头中的数据
        System.out.println(request.getHeader("User-Agent"));

        //获得请求参数
        request.getParameter("");
        request.getParameterValues("");

        Map<String,String[]> map=request.getParameterMap();

        Set<Map.Entry<String,String[]>> entrySet=map.entrySet();

        for (Map.Entry<String, String[]> entry : entrySet) {
            System.out.println(entry.getKey()+" "+ Arrays.toString(entry.getValue()));
        }


    }
}
